import * as api from "@/api";

export default {
  namespaced: true,
  state: {
    productData: [],
  },
  mutations: {
    //更改productData的mutation
    SET_PRODUCT_DATA(state, payload) {
      state.productData = payload;
    },
    //让库存减少的
    DECREASE_INVENTORY(state, payload) {
      // payload接受库存减少的这条数据的id
      console.log(payload);
      //通过payload到productData的中查找对应的数据，让inventory数量 减1
      let target = state.productData.find((item) => item.id === payload);
      if (target) {
        target.inventory--;
        
      }
    },
  },
  actions: {
    //使用action获取异步数据
    async FETCH_PRODUCT(context) {
      let res = await api.fetchProduct();
      console.log(res);
      context.commit("SET_PRODUCT_DATA", res.data);
    },
  },
  getters: {},
};
